package tech.bmatch.redpack.control;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.bmatch.redpack.model.MyRedPack;
import tech.bmatch.redpack.model.MySharedRedPack;
import tech.bmatch.redpack.model.RedPack;
import tech.bmatch.redpack.model.RedPackType;
import tech.bmatch.redpack.model.SharedRedPack;
import tech.bmatch.redpack.param.CreateRedPackParam;
import tech.bmatch.redpack.service.RedPackService;

/**
 * 外卖红包 Web API 服务
 *
 * @author joe
 * @date 2018/10/3
 */
@RestController
public class RedPackControl {

    @Autowired
    private RedPackService redPackService;

    @GetMapping("/api/redpack/query")
    public List<RedPack> query(
            @RequestParam(name = "redPackType", required = false) RedPackType redPackType) {

        if (redPackType == null) {
            redPackType = RedPackType.VOUCHER;
        }

        List<RedPack> redPacks = redPackService.query(redPackType);

        return redPacks;
    }

    @PostMapping("/api/redpack/create")
    public RedPack create(@RequestBody CreateRedPackParam packParam) {
        RedPack redPack = redPackService.create(packParam);
        return redPack;
    }

    @GetMapping("/api/myredpack/rain")
    public List<MyRedPack> rainPack(@RequestParam String userId) {
        List<MyRedPack> redPacks = redPackService.rainPack(userId);
        if (redPacks == null) {
            return new ArrayList<>();
        }
        return redPacks;
    }

    @GetMapping("/api/myredpack/createGrab")
    public String createGrab(SharedRedPack sharedRedPack) {
        SharedRedPack sharedRedPack1 = redPackService.createSharedPack(sharedRedPack);
        return sharedRedPack1.getId();
    }

    @GetMapping("/api/myredpack/grab")
    public List<MySharedRedPack> grabPack(@RequestParam(name = "userId") String userId,
                                          @RequestParam(name = "sharedId") String sharedId) {
        List<MySharedRedPack> mySharedRedPack = redPackService.grabPack(sharedId, userId);
        return mySharedRedPack;
    }
}
